<template lang="pug">
  .page
    .AccountsReceivableManage
      .search-form.mb-10.pl-10.pr-10
        Form.ivu-row.ivu-form-basic(ref="formData" :model="formData" :rules="rules" :label-width="110")
          FormItem.form-item.ivu-col.pr-20.mb-15(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
            template(v-if="item.type === 'select'")
              Select(v-model="formData[item.model]" :placeholder="item.placeholder" clearable filterable)
                Option(
                  v-for="(c_item, index) in arrListDown[item.options]"
                  :value="c_item.value"
                  :key="index"
                ) {{ c_item.label }}
            template(v-if="item.type === 'input'")
              Input(v-model="formData[item.model]" :placeholder="item.placeholder" clearable)
            template(v-if="item.type === 'radio'")
              RadioGroup.beautiful-radio-group(type="button" v-model="formData[item.model]")
                Radio.display-inline-block(v-for="c_item of item.radioArr" :label="c_item.value") {{c_item.label}}
            template(v-if="item.type === 'button'")
              Button(type="info" @click="searchFn") 查询

      .pl-20.pr-20.bg-white
        Table.hasTitle-table.beautiful-table(:columns="thead" :data="tableData" :stripe="true" ref="demoTable"
          @on-selection-change="handleOnSelectChange"
        )
          .clearfix.overflow-hidden(slot="header")
            .table-title-lefttxt.pull-left
              |合计
              span.text-info.pl-5.pr-5.font-weight {{tableComone.totalRows}}
              |条
            .pull-right.table-title-btns
              Button(v-if="hasExportList" type="info" @click="handleExport('17')") 导出
              //- .display-inline-block.mr-10
              //-   Dropdown.has-button-dropdown(trigger="click" placement="bottom-start" v-if="hasExportList || hasExportBase")
              //-     Button(type="info")
              //-       span.pr-5 导出
              //-       Icon(type="ios-arrow-down")
              //-     DropdownMenu(slot="list")
              //-       DropdownItem
              //-         Button(v-if="hasExportList" type="text" @click="handleExport('17')") 导出列表
              //-       DropdownItem
              //-         Button(v-if="hasExportBase" type="text" @click="handleExport('20')") 导出基数
              //- Dropdown.pull-right.pt-5.has-button-dropdown(placement="bottom-end" v-if="hasBatchUpdateAddStatus0 || hasBatchUpdateAddStatus5 || hasBatchUpdateReduceStatus5 || hasBatchUpateInsuredTime || hasImportInsuredTime || hasBatchUpateReduceDeclareDate || hasBatchDelete")
              //-   a(href="javascript:void(0)")
              //-     Icon.text-info.font-24(type="md-more")
              //-   DropdownMenu(slot="list")
              //-     DropdownItem
              //-       Button(v-if="hasBatchUpdateAddStatus0" type="text" @click="isBatchUpdateAddStatusModal(0)" :disabled="!selectedArr.length > 0") 新增异动未申报
              //-     DropdownItem
              //-       Button(v-if="hasBatchUpdateAddStatus5" type="text" @click="isBatchUpdateAddStatusModal(5)" :disabled="!selectedArr.length > 0") 新增异动申报成功
              //-     DropdownItem
              //-       Button(v-if="hasBatchUpdateReduceStatus5" type="text" @click="isBatchUpdateReduceStatusModal(5)" :disabled="!selectedArr.length > 0") 减少异动申报成功
              //-     DropdownItem
              //-       Button(v-if="hasBatchUpateInsuredTime" type="text" @click="handleShowSetSecurityTimeModal" :disabled="!selectedArr.length > 0") 设置参保时间
              //-     DropdownItem
              //-       Button(v-if="hasImportInsuredTime" type="text" @click="handleShowBatchImportModal") 导入并设置参保时间
              //-     DropdownItem
              //-       Button(v-if="hasBatchUpateReduceDeclareDate" type="text" @click="handleShowSetReduceDifferentTimeModal" :disabled="!selectedArr.length > 0") 设置减少异动申报时间
              //-     DropdownItem
              //-       Button(v-if="hasBatchDelete" type="text" @click="isDeleteModal" :disabled="!selectedArr.length > 0") 批量删除

          .clearfix.pl-20.pr-20(slot="footer")
            Page(
              :total="tableComone.totalRows"
              @on-change="onChangePage"
              @on-page-size-change="onpageSizeChange"
              show-sizer
              show-total
              placement="top"
              prev-text="上一页"
              next-text="下一页"
              :page-size-opts="[10, 20, 30, 50, 100]"
            )
          //- template(slot-scope="{ row, index }" slot="action")
          //-   Button.mr-10(type="text" @click="handleGoDetail(row)") 明细
          template(slot-scope="{ row, index }" slot="lookHistory")
            Button.mr-10(v-if="hasGetCommercialHistoryList" type="text" @click="handleShowCommercialHistoryModal(row)") 查看

    //- 设置申报工资
    SetDeclareWageModal(ref="SetDeclareWageModal" @query="searchFn")
    //- 设置参保时间
    SetSecurityTimeModal(ref="SetSecurityTimeModal" @query="searchFn")
    //- 导入并设置参保时间弹窗
    BatchImportModal(ref="BatchImportModal" @query="searchFn")
    //- 导入基数弹窗
    BatchImportBasicDataModal(ref="BatchImportBasicDataModal" @query="searchFn")
    //- 设置减少异动申报时间弹窗
    SetReduceDifferentTimeModal(ref="SetReduceDifferentTimeModal" @query="searchFn")
    //- 设置多个险种
    SetMoreInsuranceModal(ref="SetMoreInsuranceModal" @query="searchFn" :socialsecuritylist="arrListDown.socialsecuritylist")
    //- 查看商保替换历史
    CommercialHistoryModal(ref="CommercialHistoryModal" @query="searchFn")
</template>

<script>
import { Common } from '@/utils/common.js'
import { EmployeeInsuredApi } from '@/api/EmployeeInsured.api.js'
import { EnumApi } from '@/api/Enum.api.js'
import { CommercialBasicDataThead } from './tableHead.js'
import _cloneDeep from 'clone-deep'
import { EmployeesApi } from '@/api/Employees.api.js'
import SetDeclareWageModal from './modal/setDeclareWageModal.vue'
import SetSecurityTimeModal from './modal/setSecurityTimeModal.vue'
import BatchImportModal from './modal/batchImportModal.vue'
import BatchImportBasicDataModal from './modal/batchImportBasicDataModal.vue'
import SetReduceDifferentTimeModal from './modal/setReduceDifferentTimeModal.vue'
import SetMoreInsuranceModal from './modal/setMoreInsuranceModal.vue'
import CommercialHistoryModal from './modal/commercialHistoryModal.vue'
import config from '@/config'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro.replace('/api', '/')

export default {
  name: 'commercialBasicData', // 商保基数管理页面

  components: {
    SetDeclareWageModal,
    SetSecurityTimeModal,
    BatchImportModal,
    BatchImportBasicDataModal,
    SetReduceDifferentTimeModal,
    SetMoreInsuranceModal,
    CommercialHistoryModal
  },

  data () {
    return {
      thead: _cloneDeep(CommercialBasicDataThead),
      tableData: [],
      formData: {
        // securityType: '',
        companyName: '',
        name: '',
        idCardNos: '',
        securityNO: '',
        add_Status: '',
        reduce_Status: '',
        canReplace: ''
      },
      formItem: [
        // { type: 'select', label: '参保类型：', placeholder: '请选择参保类型', prop: 'securityType', model: 'securityType', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-8 ivu-col-span-xxl-6', options: 'SecurityType' },
        // { type: 'input', label: '公司名称：', placeholder: '请输入公司名称', prop: 'companyName', model: 'companyName', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-8 ivu-col-span-xxl-6' },
        { type: 'input', label: '员工名称：', placeholder: '请输入员工名称', prop: 'name', model: 'name', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-8 ivu-col-span-xxl-6' },
        { type: 'input', label: '身份证号码：', placeholder: '请输入身份证号码', prop: 'idCardNos', model: 'idCardNos', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-8 ivu-col-span-xxl-6' },
        { type: 'input', label: '参保编号：', placeholder: '请输入参保编号', prop: 'securityNO', model: 'securityNO', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-8 ivu-col-span-xxl-6' },
        { type: 'select', label: '新增异动状态：', placeholder: '请选择新增异动状态', prop: 'add_Status', model: 'add_Status', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-8 ivu-col-span-xxl-6', options: 'declarestatus' },
        { type: 'select', label: '减少异动状态：', placeholder: '请选择减少异动状态', prop: 'reduce_Status', model: 'reduce_Status', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-8 ivu-col-span-xxl-6', options: 'declarestatus' },
        { type: 'radio', label: '是否可替换：', placeholder: '请选择是否可替换', prop: 'canReplace', model: 'canReplace', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-8 ivu-col-span-xxl-6', radioArr: [{ value: '', label: '全部' }, { value: 'true', label: '是' }, { value: 'false', label: '否' }] },
        { type: 'button', style: 'ivu-col-span-md-24 ivu-col-span-lg-12 ivu-col-span-xl-4 ivu-col-span-xxl-4' }
      ],
      rules: {},
      selectedId: [], // 勾选的id集合,array格式
      selectedArr: [], // 勾选的id, name数组集合
      tableComone: {
        pageIndex: 1, // 页码
        totalRows: 0, // 总条数
        pageSize: 10, // 当前页面展示条数
      },
      arrListDown: {
        declarestatus: [],
        socialsecuritylist: []
      }
    }
  },

  computed: {
    actionArr () {
      let arr = this.$store.state.user.actionAccess.map(item => {
        return item.code
      })
      return arr
    },
    // 新增异动未申报
    hasBatchUpdateAddStatus0 () {
      return this.actionArr.indexOf('BatchUpdateAddStatus0') > -1
    },
    // 新增异动申报成功
    hasBatchUpdateAddStatus5 () {
      return this.actionArr.indexOf('BatchUpdateAddStatus5') > -1
    },
    // 减少异动申报成功
    hasBatchUpdateReduceStatus5 () {
      return this.actionArr.indexOf('BatchUpdateReduceStatus5') > -1
    },
    // 设置申报工资
    hasBatchUpateDeclareSalary () {
      return this.actionArr.indexOf('BatchUpateDeclareSalary') > -1
    },
    // 设置参保时间
    hasBatchUpateInsuredTime () {
      return this.actionArr.indexOf('BatchUpateInsuredTime') > -1
    },
    // 导入并设置参保时间
    hasImportInsuredTime () {
      return this.actionArr.indexOf('ImportInsuredTime') > -1
    },
    // 导入多个客户基数
    hasImportInsured () {
      return this.actionArr.indexOf('ImportInsured') > -1
    },
    // 设置减少异动申报时间
    hasBatchUpateReduceDeclareDate () {
      return this.actionArr.indexOf('BatchUpateReduceDeclareDate') > -1
    },
    // 设置多个险种
    hasBatchDeleteSecurity () {
      return this.actionArr.indexOf('BatchDeleteSecurity') > -1
    },
    // 批量删除
    hasBatchDelete () {
      return this.actionArr.indexOf('BatchDelete') > -1
    },
    // 导出列表
    hasExportList () {
      return this.actionArr.indexOf('ExportList') > -1
    },
    // 导出基数
    hasExportBase () {
      return this.actionArr.indexOf('ExportBase') > -1
    },
    // 导出基数模板
    hasExportBaseTemplate () {
      return this.actionArr.indexOf('ExportBaseTemplate') > -1
    },
    // 查看商保替换历史
    hasGetCommercialHistoryList() {
      return this.actionArr.indexOf('GetCommercialHistoryList') > -1
    }
  },

  mounted () {    
    this.getEnum()
    this.query()
  },

  methods: {
    // 数据字典
    getEnum () {
      let groupName = 'employeeinsured.declarestatus,employeeinsured.socialsecuritylist'
      Common.getEnum3(groupName, this.arrListDown)
      console.log('数据字典：', this.arrListDown)
    },

    // 搜索
    searchFn () {
      this.tableComone.pageIndex = 1
      this.query()
    },

    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      this.selectedId = []
      this.selectedArr = []
      var params = {
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      // this.formData.canReplace = this.formData.canReplace !== '' ? Boolean(this.formData.canReplace) : ''
      let res = await EmployeeInsuredApi.getCommercialList ({ ...params, ...this.formData })
      console.log('社保公积金基数管理res: ', res)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
        this.tableComone.totalMoney = res.data.totalMoney
      } else {
        this.tableData = []
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
      this.loading = false
    },

    // 表格选择操作
    handleOnSelectChange (value) {
      this.selectedArr = value.map((item) => {
        return { id: item.id, name: item.name }
      })
      this.selectedId = value.map((item) => {
        return item.id
      })
      console.log('列表勾选数组1', this.selectedArr)
      console.log('列表勾选数组2', this.selectedId)
    },

    // 导出
    async handleExport (exportType) {
      var params = {ids: this.selectedId.join(',')}
      let res = await EmployeeInsuredApi.export(exportType, {...this.formData, ...params})
      console.log('导出res: ', res)
      if (res.status === 200 && res.data.success) {
        window.open(baseUrl + res.data.data, '_blank')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 新增异动未申报-操作前询问
    isBatchUpdateAddStatusModal (addStatus) {
      this.$Modal.confirm({
        title: '确认是否调整所有查询结果的新增异动申报状态？',
        width: 500,
        onOk: () => {
          this.handleSureBatchUpdateAddStatus(addStatus)
        }
      })
    },

    // 新增异动未申报-确定
    async handleSureBatchUpdateAddStatus (addStatus) {
      var params = {
        Ids: this.selectedId.join(','),
        SecurityType: 3, // 商保传3
        Add_Status: addStatus
      }
      console.log('params: ', params)
      let res = await EmployeeInsuredApi.batchUpdateAddStatus(params)
      console.log('新增异动未申报res: ', res)
      if (res.status === 200 && res.data.success) {
        this.$Message.success(res.data.message)
        this.query()
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 减少异动申报成功-操作前询问
    isBatchUpdateReduceStatusModal (addStatus) {
      this.$Modal.confirm({
        title: '确认是否调整所有查询结果的减少异动申报状态？',
        width: 500,
        onOk: () => {
          this.handleSureBatchUpdateReduceStatus(addStatus)
        }
      })
    },

    // 减少异动申报成功-确定
    async handleSureBatchUpdateReduceStatus (addStatus) {
      var params = {
        Ids: this.selectedId.join(','),
        SecurityType: 3, // 商保传3
        Add_Status: addStatus
      }
      console.log('params: ', params)
      let res = await EmployeeInsuredApi.batchUpdateReduceStatus(params)
      console.log('减少异动申报成功res: ', res)
      if (res.status === 200 && res.data.success) {
        this.$Message.success(res.data.message)
        this.query()
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 是否删除前询问
    isDeleteModal () {
      this.$Modal.confirm({
        title: '确认是否删除所有查询结果',
        onOk: () => {
          this.handleSureDelete()
        }
      })
    },

    // 删除查询结果-确认删除
    async handleSureDelete () {
      var params = {
        Ids: this.selectedId.join(','),
        SecurityType: 3, // 商保传3
      }
      let res = await EmployeeInsuredApi.batchDelete(params)
      console.log('res: ', res)
      if (res.status === 200 && res.data.success) {
        this.$Message.success(res.data.message)
        this.query()
      } else {        
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    },

    // 设置申报工资-显示弹窗
    handleShowSetDeclareWageModal () {
      this.$refs.SetDeclareWageModal.ids = this.selectedId
      this.$refs.SetDeclareWageModal.securityType = 3
      this.$refs.SetDeclareWageModal.show()
    },

    // 设置参保时间-显示弹窗
    handleShowSetSecurityTimeModal () {
      this.$refs.SetSecurityTimeModal.ids = this.selectedId
      this.$refs.SetSecurityTimeModal.securityType = 3
      this.$refs.SetSecurityTimeModal.show()
    },

    // 设置减少异动申报时间-显示弹窗
    handleShowSetReduceDifferentTimeModal () {
      this.$refs.SetReduceDifferentTimeModal.ids = this.selectedId
      this.$refs.SetReduceDifferentTimeModal.securityType = 3
      this.$refs.SetReduceDifferentTimeModal.show()
    },

    // 设置多个险种-显示弹窗
    handleShowSetMoreInsuranceModal () {
      this.$refs.SetMoreInsuranceModal.ids = this.selectedId
      this.$refs.SetMoreInsuranceModal.securityType = 3
      this.$refs.SetMoreInsuranceModal.show()
    },

    // 导入并设置参保时间-显示弹窗
    handleShowBatchImportModal () {
      this.$refs.BatchImportModal.file = null
      this.$refs.BatchImportModal.errorData.isError = false
      this.$refs.BatchImportModal.show()
    },

    // 导入基数-显示弹窗
    handleShowBatchImportBasicDataModal () {
      this.$refs.BatchImportBasicDataModal.file = null
      this.$refs.BatchImportBasicDataModal.errorData.isError = false
      this.$refs.BatchImportBasicDataModal.isShowBatchImportBasicDataModal = true
    },

    // 查看商保替换历史-显示弹窗
    handleShowCommercialHistoryModal (row) {
      this.$refs.CommercialHistoryModal.query(row.orderNo)
    }
  } // methods end
}
</script>

<style lang="less" scoped>

</style>
